{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#考点：动态规划\n",
    "#从左上角单元到第一行和第一列的单元，path数都是1，\n",
    "#从左上角单元到其余单元的path数均为其左边和上边的和\n",
    "class Solution:\n",
    "    def uniquePaths(self, m: int, n: int) -> int:\n",
    "        path = [[1]*m for i in range(n)]\n",
    "        for i in range(1, n):\n",
    "            for j in range(1, m):\n",
    "                path[i][j] = path[i-1][j] + path[i][j-1]\n",
    "        return path[n-1][m-1]\n",
    " \n",
    "# memory better solution:\n",
    "#A 2D aux array is redundant. 1D is enough.\n",
    "class Solution:\n",
    "    def uniquePaths(self, m: int, n: int) -> int:\n",
    "        dp = [1] * n\n",
    "        for i in range(1, m):\n",
    "            for j in range(1, n):\n",
    "                dp[j] = dp[j - 1] + dp[j]\n",
    "        return dp[-1] if m and n else 0"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
